package com.sec.etech.employeesync.handler;

import com.sec.etech.employeesync.entity.OrgEmployeeZSecondaryLevelJob;
import com.sec.etech.employeesync.mapper.OrgEmployeeZSecondaryLevelJobMapper;
import com.sec.etech.employeesync.util.ConfigHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SecondaryLevelJobHandler implements ConfigHandler {

    @Resource
    private OrgEmployeeZSecondaryLevelJobMapper secondaryLevelJobMapper;


    @Override
    public Long createConfig(String... params) {
        String code = params[0];
        String name = params[1];
        OrgEmployeeZSecondaryLevelJob secondaryLevelJob = new OrgEmployeeZSecondaryLevelJob();
        secondaryLevelJob.setCode(code);
        secondaryLevelJob.setName(name);
        secondaryLevelJob.setActive(1);
        secondaryLevelJobMapper.insert(secondaryLevelJob);

        log.info("新增二级岗位配置:name={}", name);

        return secondaryLevelJob.getSecondaryLevelJobId();
    }

    @Override
    public List<ConfigEntry> getAllConfigs() {
        return secondaryLevelJobMapper.selectList(null).parallelStream()
                .map(item -> {
                    ConfigEntry entry = new ConfigEntry();
                    entry.setId(item.getSecondaryLevelJobId());
                    entry.setParams(new String[]{item.getCode(), item.getName()});
                    return entry;
                })
                .collect(Collectors.toList());
    }
}
